package cn.itrip.auth.service;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.beans.vo.ItripTokenVO;
import cn.itrip.beans.vo.userinfo.ItripUserVO;
import cn.itrip.common.DateUtil;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.RedisAPI;
import cn.itrip.commons.MD5;
import cn.itrip.dao.user.ItripUserMapper;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.Objects;

/**
 * @author Super God Jie
 * @version 1.0  2022-05-05 15:25
 */
@Service
public class UserService {
	@Resource
	private ItripUserMapper itripUserMapper;
	@Resource
	private MailANDSMSService mailANDSMSService;
	@Resource
	private RedisAPI redisAPI;

	public Dto login(String name, String password, String userAgent) {
		ItripUser itripUser = itripUserMapper.queryUser(name);
		Assert.notNull(itripUser,"用户名不正确");
		Boolean flag = MD5.md5(password).equals(itripUser.getUserPassword());
		Assert.isTrue(flag,"密码不正确");
		//token
		String token = this.createToken(itripUser, userAgent);
		long currentTime = System.currentTimeMillis();
		if(userAgent.contains("Windows")){
			redisAPI.set(token,7200, JSONObject.toJSONString(itripUser));
		}else {
			redisAPI.set(token, JSONObject.toJSONString(itripUser));
		}
		ItripTokenVO itripTokenVO = new ItripTokenVO();
		itripTokenVO.setToken(token);
		itripTokenVO.setExpTime((currentTime+7200)*1000);
		itripTokenVO.setGenTime(currentTime);
		return DtoUtil.returnDataSuccess(itripTokenVO);
	}

	public String createToken(ItripUser itripUser,String userAgent){
		StringBuilder sb = new StringBuilder("token:");
		if(userAgent.contains("Windows")){
			sb.append("PC-");
		}else {
			sb.append("MOBILE-");
		}
		try {
			sb.append(MD5.md5(itripUser.getUserCode())).append("-")
			.append(itripUser.getId()).append("-")
			.append(DateUtil.format(new Date(),"yyyyMMddHHmmss")).append("-")
			.append(RandomStringUtils.randomAlphanumeric(4));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	public Dto checkUserCode(String name) {
		ItripUser itripUser = itripUserMapper.checkUserCode(name);
		if(Objects.isNull(itripUser)){
			return DtoUtil.returnSuccess("用户名可用");
		}
		return DtoUtil.returnFail("用户名不可用","100001");
	}

	public Dto register(ItripUserVO itripUserVO, String type) {
		String activeCode = RandomStringUtils.randomNumeric(4);
		itripUserVO.setUserPassword(MD5.md5(itripUserVO.getUserPassword()));
		itripUserMapper.insertUserVO(itripUserVO);
		switch (type){
			case "mail":
				mailANDSMSService.sendMail(itripUserVO.getUserCode(),activeCode);
				break;
			case "phone":
				mailANDSMSService.sendPhone(itripUserVO.getUserCode(),activeCode);
				break;
		}
		redisAPI.set(itripUserVO.getUserCode(),5*60,activeCode);
		return DtoUtil.returnSuccess("注册成功");
	}

	public Dto active(String user, String code) {
		String codes = redisAPI.get(user);
		if (redisAPI.ttl(user) == -1) {
			return DtoUtil.returnFail("激活已过期","10003");
		}
		if(!codes.equals(code)){
			return DtoUtil.returnFail("激活码错误","10003");
		}
		itripUserMapper.activeUser(user,1);
		return DtoUtil.returnSuccess("激活成功");
	}
}
